import { menuList } from './menuList.json'

console.log(menuList)

const generateTreeStructure = (menuList: any, id: number) => {
  const newMenuList: any[] = []
  findPermissionMenu().forEach((menu: any) => {
    if (menu.pid === id) {
      menu.children = generateTreeStructure(menuList, menu.id)
      newMenuList.push(menu)
    }
  })
  return newMenuList;
}

const permissionIds = [9, 15, 16, 17, 18, 1, 7]

const findPermissionMenu = () => {
  let newMenu: any[] = []
  menuList.forEach(menu => {
    if (permissionIds.includes(menu.id)) {
      newMenu.push(menu)
    }
  })
  return newMenu
}

console.log(findPermissionMenu())


console.log(generateTreeStructure(menuList, 0))
// generateTreeStructure(menuList, 1)


const data = [
  {
    name: 1,
    children: [
      {
        name: '1-1',
        children: [
          {
            name: '1-1-1'
          }
        ]
      },
      {
        name: '1-2',
        children: [
          {
            name: '1-2-1'
          }
        ]
      },
    ]
  }, {
    name: 2,
    children: [
      {
        name: '2-1',
        children: [
          {
            name: '2-1-1'
          }
        ]
      },
      {
        name: '2-2',
        children: [
          {
            name: '2-2-1'
          }
        ]
      },
    ]
  }
]

console.log(data)


const aa = (data: any[]) => {
  const newData: any[] = []
  data.forEach((item: any) => {
    if (item.children && item.children.length > 0) {
      newData.push({ ...item, children: null })
    } else {
      newData.push(item)
    }
  })
  console.log(newData)
  return newData
}

aa(data)

